<?php

namespace Admin\Controller;

class DoctorController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Doctor");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取医生列表
        $list = $this->currentModel->queryForList($page, $requestMap);

        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function edit() {

        $id = $_REQUEST["id"];

        $doctor = $this->currentModel->queryById($id);

        $categoryList = D("Category")->select();

        $doctorCategoryMap["doctor_id"] = $id;
        $doctorCategoryList = D("DoctorCategory")->where($doctorCategoryMap)->select();

        $this->assign("doctorCategoryList", $doctorCategoryList);
        $this->assign("categoryList", $categoryList);
        $this->assign("info", $doctor);
        $this->display("edit");

    }

    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];

        $doctorData = D("Doctor")->queryById($id);

        $doctorData["customer_id"] = $_REQUEST["customerId"];
        $doctorData["doctor_name"] = $_REQUEST["doctorName"];
        $doctorData["hospital_name"] = $_REQUEST["hospitalName"];
        $doctorData["image_url"] = $_REQUEST["imageUrl"];
        $doctorData["city_id"] = $_REQUEST["cityId"];
        //$doctorData["description"] = html_entity_decode($_REQUEST["description"]);
        $doctorData["good_at"] = html_entity_decode($_REQUEST["goodAt"]);
        $doctorData["expirence"] = html_entity_decode($_REQUEST["expirence"]);
        $doctorData["doctor_link"] = $_REQUEST["doctorLink"];
        $doctorData["brief"] = $_REQUEST["brief"];

        D("Doctor")->updateInfo($doctorData);

        $categoryIds = I('post.categoryIds');

        $categoryIdArray = explode(',', $categoryIds);

        $doctorCategoryMap['doctor_id'] = $id;

        D("DoctorCategory")->where($doctorCategoryMap)->delete();

        foreach ($categoryIdArray as $categoryId) {

            $doctorCategoryData["doctor_id"] = $id;
            $doctorCategoryData["category_id"] = $categoryId;
            $doctorCategoryData["create_time"] = getCurrentDate();

            D("DoctorCategory")->add($doctorCategoryData);

        }

        $this->success("更新成功", U("doctor/index"));

    }

    public function notifyDoctor() {

        $id = $_REQUEST["id"];

        $this->assign("id", $id);
        $this->display();

    }

    public function remove() {

        $id = $_REQUEST["id"];

        $doctor = D("Doctor")->queryById($id);
        $customer = D("Customer")->queryById($doctor["customer_id"]);

        $customer["is_doctor"] = -1;
        D("Customer")->updateInfo($customer);

        D("Doctor")->deleteById($id);

        $this->success("删除成功！", U("doctor/index"));

    }

    public function notifyDoctorPost() {

        $id = $_REQUEST["id"];
        $doctor = D("Doctor")->queryById($id);

        $customer = D("Customer")->queryById($doctor["customer_id"]);

        $data["openid"] = $customer["openid"];
        $data["title"] = $_REQUEST["title"];
        $data["orderNumber"] = $_REQUEST["orderNumber"];
        $data["remark"] = $_REQUEST["remark"];
        $data["serviceName"] = $_REQUEST["serviceName"];
        $data["count"] = $_REQUEST["count"];
        $data["price"] = $_REQUEST["price"];

        wechatNotifyDoctor($data);

        $this->success("发送成功", U("doctor/index"));

    }

}